<?php
/**
 * フィルター通した後の検証用
 * 
 * なんでZendFilterInputのサンプルにあるのに実際のクラスにないの・・・
 * 
 * array(
 * 	new Zx_Validate_StringEqual(),
 * 	Zend_Filter_Input::FIELDS => array('MAILADDRESS', 'MAILADDRESS2'),
 * )
 * 
 * @author tanaka
 * @package Zx_Validate
 * @subpackage StringEqual
 */
class Validate_StringEqual extends Zend_Validate_Abstract
{
	/**
	 * Error codes
	 * @const string
	 */
	const NOT_MATCH = 'notMatch';
	const UNABLE_TO_MATCH = 'unableToMatch';

	/**
	 * Error messages
	 * @var array
	 */
	protected $_messageTemplates = array(
		"%field1% and %field2% are not equal.",
		self::UNABLE_TO_MATCH => "One or both strings are missing.",
		self::NOT_MATCH => "One or both strings are missing."
	);

	/**
	 * @var array
	 */
	protected $_messageVariables = array(
		'field1' => '_field1',
		'field2' => '_field2'
	);

	protected $_case = false;
	protected $_field1 = null;
	protected $_field2 = null;

	/**
	 * Sets validator options
	 *
	 * @param  boolean $case
	 * @return void
	 */
	public function __construct($case = false)
	{
		$this->_case = $case;
	}

	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if the the 2 strings are equal
	 *
	 * @param  array $value
	 * @return boolean
	 */
	public function isValid($value)
	{
		if (!is_array($value) OR sizeof($value) < 2) {
			$this->_error(self::UNABLE_TO_MATCH);
		}

		$this->_field1 = array_shift($value);
		$this->_field2 = array_shift($value);

		if ($this->_case === true) $function = 'strcmp';
		else $function = 'strcasecmp';

		if (0 !== $function($this->_field1, $this->_field2)) $this->_error(self::NOT_MATCH);

		if (count($this->_messages)) {
			return false;
		}
		else {
			return true;
		}
	}
}
